#include "GraphicalComponent.h"
#include "ModelFactory.h"
#include "Library.h"

const HashedString GraphicalComponent::type("GraphicalComponent");

GraphicalComponent::GraphicalComponent( Entity* parent, Pointer<Model>& model ) : IComponent(parent)
{
	m_modelNode = new ModelNode(CreateModelInstance(model));
}

std::string GraphicalComponent::ToString() const
{
	ModelNode* modelNode = (ModelNode*)((INode*)m_modelNode);
	Model* model = modelNode->getModelInstance()->getModel();
	std::string string("Model=" + Library::getSingleton().getResourceName(Model::type, (IResource*)model).getString());
	return string;
}

GraphicalComponent::~GraphicalComponent()
{
	if (m_modelNode.isValid())
		m_modelNode->Remove();
}

const HashedString& GraphicalComponent::Type()
{
	return type;
}

Pointer<INode>& GraphicalComponent::getNode()
{
	return m_modelNode;
}

void GraphicalComponent::Remove()
{
	if (m_modelNode.isValid())
		m_modelNode->Remove();
}

bool GraphicalComponent::ToXML( tinyxml2::XMLPrinter& printer )
{
	ModelNode* modelNode = (ModelNode*)((INode*)m_modelNode);
	Model* model = modelNode->getModelInstance()->getModel();
	std::string string(Library::getSingleton().getResourceName(Model::type, (IResource*)model).getString());
	
	printer.OpenElement("Model");
	printer.PushAttribute("Name", string.c_str());
	printer.CloseElement();

	return true;
}